﻿using System;
using System.Web.UI.WebControls;
using Alogient.Cameleon.Forms.Sdk.Items;
using System.Data.Common;

namespace Alogient.Cameleon.Forms.Sdk.Controls
{
    public class CMFormSurvey : Panel
    {
        public CMFormSurvey (Form form, Submission submission)
        {
            if (submission != null)
            {                
                foreach (FormPage page in form.Pages)
                {
                    foreach (FormElement element in page.Elements)
                    {
                        switch (element.InputType)
                        {
                            case FormInputType.Radio:
                            case FormInputType.Checkbox:
                                int choicesTotal = FormElementChoice.GetFormElementChoicesTotal(element.ID, null, null, null, null);

                                Panel questionPanel = new Panel();
                                questionPanel.CssClass = "surveyResults";

                                Label label = new Label();
                                label.CssClass = "surveyResultsTitle";
                                label.Text = element.Label;
                                questionPanel.Controls.Add(label);

                                int graphId = 1;

                                foreach (DbDataRecord record in FormElementChoice.GetFormElementChoicesCount(element.ID, null, null, null, null))
                                {
                                    Panel panel = new Panel();
                                    panel.CssClass = "surveyResultsLine";
                                    
                                    Label nameLabel = new Label();
                                    nameLabel.CssClass = "surveyResultsName";
                                    Label valueLabel = new Label();
                                    valueLabel.CssClass = "surveyResultsValue";
                                    Label percentLabel = new Label();
                                    percentLabel.CssClass = "surveyResultsPercent";
                                    Label graphLabel = new Label();
                                    graphLabel.CssClass = "surveyGraph" + graphId++;

                                    //int otherTotal = FormElementChoice.GetFormElementOtherChoicesTotal(element.ID, null, null, null, null);

                                    int choiceID = (int)record["Form_Element_Choice_ID"];
                                    int count = (int)record["COUNT"];

                                    FormElementChoice choice = new FormElementChoice(choiceID, submission.CultureCode);
                                    nameLabel.Text = choice.Name;
                                    valueLabel.Text = count.ToString();

                                    foreach (SubmissionValue value in submission.values)
                                    {
                                        if (value.FormElementID == choice.FormElementID)
                                        {
                                            if (element.InputType == FormInputType.Radio)
                                            {
                                                if (value.Value == choice.ID.ToString())
                                                    panel.Attributes.CssStyle.Add("font-weight", "bold");
                                            }
                                            else if (element.InputType == FormInputType.Checkbox)
                                            {
                                                if (value.Value.IndexOf(';') > -1)
                                                {
                                                    string[] values = value.Value.Split(';');
                                                    foreach (string s in values)
                                                    { 
                                                        if (s == choice.ID.ToString())
                                                            panel.Attributes.CssStyle.Add("font-weight", "bold");
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    if (choicesTotal > 0)
                                    {
                                        // display the percentage as text and set the width of the percent bar 
                                        // to a maximum of 80% of its available space to display the text after it
                                        double percent = ((count / (choicesTotal * 1.0)) * 100);
                                        percentLabel.Text = Math.Round(percent, Util.ConfigurationUtils.SurveyResultDigits) + "%";
                                        graphLabel.Style.Add("width", Math.Round(percent * 0.8) + "%");
                                    }
                                    else
                                    {
                                        percentLabel.Text = "0";
                                    }

                                    panel.Controls.Add(nameLabel);
                                    panel.Controls.Add(graphLabel);
                                    panel.Controls.Add(percentLabel);
                                    panel.Controls.Add(valueLabel);

                                    questionPanel.Controls.Add(panel);
                                }

                                //Add Other
                                bool hasOther = false;
                                string otherLabel = string.Empty;

                                if (element.InputType == FormInputType.Radio)
                                {
                                    Radio radio = element as Radio;
                                    if (radio != null && radio.Other)
                                    {
                                        hasOther = true;
                                        if (!string.IsNullOrEmpty(radio.OtherLabel))
                                            otherLabel = radio.OtherLabel;
                                    }
                                }
                                else if (element.InputType == FormInputType.Checkbox)
                                {
                                    Checkbox checkbox = element as Checkbox;
                                    if (checkbox != null && checkbox.Other)
                                    {
                                        hasOther = true;
                                        if (!string.IsNullOrEmpty(checkbox.OtherLabel))
                                            otherLabel = checkbox.OtherLabel;
                                    }
                                }

                                if (hasOther)
                                {
                                    int otherTotal = FormElementChoice.GetFormElementOtherChoicesTotal(element.ID, null, null, null, null);
                                    Panel panel = new Panel();
                                    panel.CssClass = "surveyResultsLine";

                                    Label nameLabel = new Label();
                                    nameLabel.CssClass = "surveyResultsName";
                                    Label valueLabel = new Label();
                                    valueLabel.CssClass = "surveyResultsValue";
                                    Label percentLabel = new Label();
                                    percentLabel.CssClass = "surveyResultsPercent";
                                    Label graphLabel = new Label();
                                    graphLabel.CssClass = "surveyGraph" + graphId++;

                                    nameLabel.Text = otherLabel;
                                    valueLabel.Text = otherTotal.ToString();

                                    if (choicesTotal > 0)
                                    {
                                        // display the percentage as text and set the width of the percent bar 
                                        // to a maximum of 80% of its available space to display the text after it
                                        double percent = ((otherTotal / (choicesTotal * 1.0)) * 100);
                                        percentLabel.Text = Math.Round(percent, Util.ConfigurationUtils.SurveyResultDigits) + "%";
                                        graphLabel.Style.Add("width", Math.Round(percent * 0.8) + "%");
                                    }
                                    else
                                    {
                                        percentLabel.Text = "0";
                                    }

                                    panel.Controls.Add(nameLabel);
                                    panel.Controls.Add(graphLabel);
                                    panel.Controls.Add(percentLabel);
                                    panel.Controls.Add(valueLabel);

                                    questionPanel.Controls.Add(panel);
                                }

                                label = new Label();
                                label.CssClass = "surveyResultsSubTitle";
                                label.Text = Util.WebUtil.GetLabelsRessource("TotalAnswersForQuestion");
                                questionPanel.Controls.Add(label);

                                label = new Label();
                                label.CssClass = "surveyResultsSubTitleTotal";
                                label.Text = choicesTotal.ToString();
                                questionPanel.Controls.Add(label);

                                this.Controls.Add(questionPanel);

                                break;
                        }
                    }
                }
            }
        }
    }
}
